package com.xcf.controller.business.finance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import com.xcf.controller.base.BaseController;
import com.xcf.controller.base.ExportDataHandler;
import com.xcf.entity.Page;
import com.xcf.service.business.finance.CostCheckListService;
import com.xcf.util.DateUtil;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value = "/costCheckList")
public class CostCheckListController extends BaseController implements ExportDataHandler{
	@Resource
	private CostCheckListService costCheckListService;
	@RequestMapping(value="/list")
	public ModelAndView CostCheckListPage()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/finance/paymentManage/cost_check_list");
		return view;
		
	}
	/**
	 * 数据查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findPaymentInfo")
	@ResponseBody
	public ResponseResult findPaymentInfo(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = costCheckListService.findInfo(pd);
//		List<Map> list = new ArrayList<>();
//		List<Map> all = costCheckListService.findPaymentInfo(pd);
//		List<Map> loan = costCheckListService.findLoanInfo(pd);
		//只有付款的时候
//		if(all.size()>0 && loan.size()<=0){
//			
//			List<Map> listAll = new ArrayList<>();
//			for(Map p:all){
//				p.put("bill_code", p.get("bill_code"));
//				p.put("invoice_code", p.get("invoice_code"));
//				p.put("payment_amount", p.get("amount"));
//				p.put("payment_bill_date", p.get("payment_bill_date"));
//				p.put("fee_name", p.get("fee_name"));
//				p.put("is_offset", p.get("is_offset"));
//				listAll.add(p);
//			}
//			list=listAll;
//		}
		//只有预付的时候
//		if(loan.size()>0 && all.size()<=0){
//			List<Map> listloan = new ArrayList<>();
//			for(Map p:loan){
//				p.put("bill_code", p.get("bill_code"));
//				p.put("payment_bill_date", p.get("payment_bill_date"));
//				p.put("loan_amount", p.get("amount"));
//				listloan.add(p);
//			}
//			list=listloan;
//		}
		//两个都存在的时候
//		if(all.size()>0 && loan.size()>0){
//		for(Map m : all){
////			if(m.get("invoice_code")==null || m.get("invoice_code")==""){
//			if(loan.size()>0){
//				for(int i =0;  i<=loan.size();i++){
//					if(i>=loan.size()) break;
//					if(m.get("bill_code").equals(loan.get(i).get("bill_code"))){
//						Map mp = new HashMap<>();
//						mp.put("id", m.get("id"));
//						mp.put("payment_app_code", m.get("payment_app_code"));
//						mp.put("bill_code", m.get("bill_code"));
//						if(m.get("invoice_code")==null ||m.get("invoice_code")==""){
//							mp.put("payment_amount", null);
//							mp.put("loan_amount", m.get("amount"));
//						}else{
//							mp.put("payment_amount", m.get("amount"));
//						}
//						mp.put("fee_name", m.get("fee_name"));
//						mp.put("invoice_code", m.get("invoice_code"));
////						mp.put("loan_amount", loan.get(i).get("amount"));
//						mp.put("payment_bill_date", loan.get(i).get("payment_bill_date"));
//						list.add(mp);
//						loan.remove(loan.get(i));
//						
//						break;
//					}else{
//						if(m.get("invoice_code")==null ||m.get("invoice_code")==""){
//							m.put("payment_amount", null);
//							m.put("loan_amount", loan.get(i).get("amount"));
//						}else{
//							m.put("payment_amount", m.get("amount"));
//						}
//						list.add(m);
//						list.add(loan.get(i));
//					}
//					
//				}
//			}else{
//				m.put("payment_amount", m.get("amount"));
//				list.add(m);
//			}
//			
//		}
//		}
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 收入数据查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findIncomeInfoBy")
	@ResponseBody
	public ResponseResult findIncomeInfoBy(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = costCheckListService.findIncomeInfoBy(pd);
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 数据查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findPaymentInfoWhere")
	@ResponseBody
	public ResponseResult findPaymentInfoWhere(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = costCheckListService.findPaymentInfoWhere(pd);
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 数据查询
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/internalTradeBaseInfo")
	@ResponseBody
	public ResponseResult internalTradeBaseInfo(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = costCheckListService.findCostCheckList(pd);
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 付款单号
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findPaymentNum")
	@ResponseBody
	public ResponseResult findPaymentNum(int page) throws Exception{
		PageHelper.startPage(page, 10);
		PageData pd = new PageData();
		pd = this.getPageData();
		List<Map> list = costCheckListService.findPaymentNum(pd);
		return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 发票查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findInvoice")
	@ResponseBody
	public ResponseResult findInvoice()throws Exception{
		@SuppressWarnings("rawtypes")
		List<Map> list = costCheckListService.findInvoice();
		return ResponseResult.success(new PageInfo(list));
		
	}
	/**
	 * 托单编号查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findBillCode")
	@ResponseBody
	public ResponseResult findBillCode()throws Exception{
		List<Map> list = costCheckListService.findBillCode();
		return ResponseResult.success(new PageInfo(list));
		
	}
	/**
	 * 部门查询
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/findDepartment")
	@ResponseBody
	public ResponseResult findDepartment()throws Exception{
		List<Map> list = costCheckListService.findDepartment();
		return ResponseResult.success(new PageInfo(list));
		
	}
	/**
	 * 保存信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveInfo")
	@ResponseBody
	public String saveInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		String pp1 = null;
		if(pd.getString("payment_app_code")==null||pd.getString("payment_app_code")==""||pd.getString("payment_app_code").equals("null")||pd.getString("payment_app_code").equals("")){
			PageData b = new PageData();
			b.put("paymentCode", null);
			Object pp = costCheckListService.getPaymentCode(b);
			pp1 = pp.toString();
			
		}else{
			pp1 = pd.getString("payment_app_code");
		}
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
//			PageData pdq = new PageData();
//			pdq.put("is_loan", pd.getString("is_loan"));
//			pdq.put("settlement_dept", pd.getString("settlement_dept"));
//			pdq.put("deposit_bank", pd.getString("deposit_bank"));
//			pdq.put("bank_account", pd.getString("bank_account"));
//			pdq.put("payment_term", pd.getString("payment_term"));
//			if(pd.getString("payment_bill_date")!=null&&pd.getString("payment_bill_date")!=""){
//				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//				String dstr=pd.getString("payment_bill_date");  
//				java.util.Date date=sdf.parse(dstr);
//				pdq.put("payment_bill_date", date);
//			}
//			pdq.put("payment_app_dept", pd.getString("payment_app_dept"));
//			pdq.put("dept_business_type", pd.getString("dept_business_type"));
//			pdq.put("invoice_code", item.getString("invoice_code"));
//			pdq.put("bill_code", item.getString("bill_code"));
//			if(item.getString("payment_amount")!=null&&item.getString("payment_amount")!=""){
//				float a = Float.parseFloat(item.getString("payment_amount"));
//				
//				pdq.put("payment_amount", a);
//			}
//			pdq.put("fee_name", item.getString("fee_name"));
//			
//			if(!item.getString("loan_odd_code").isEmpty()){
//				pdq.put("loan_odd_code", item.getString("loan_odd_code"));
//			}
//			if(!item.getString("loan_date").isEmpty()){
//				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//				String dstr=item.getString("loan_date");  
//				java.util.Date date=sdf.parse(dstr);
//				pdq.put("loan_date", date);
//			}
//			if(!item.getString("loan_amount").isEmpty()){
//				float a = Float.parseFloat(item.getString("loan_amount"));
//				
//				pdq.put("loan_amount", a);
//			}
//			
//			if(!item.getString("receipt_code").isEmpty()){
//				pdq.put("receipt_code", item.getString("receipt_code"));
//			}
//			if(!item.getString("receipt_date").isEmpty()){
//				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
//				String dstr=item.getString("receipt_date");  
//				java.util.Date date=sdf.parse(dstr);
//				pdq.put("receipt_date", date);
//			}
//			if(!item.getString("receipt_amount").isEmpty()){
//				float a = Float.parseFloat(item.getString("receipt_amount"));
//				
//				pdq.put("receipt_amount", a);
//			}
//			pdq.put("remark", item.getString("remark"));
//			pdq.put("payment_app_code", item.getString("payment_app_code"));
//			if(!item.getString("id").isEmpty()){
//				pdq.put("id", item.getString("id"));	
//			}
//			pdq.put("currency", item.getString("currency"));
			
//			if(!item.getString("id").isEmpty()){
//				costCheckListService.updateInfo(pdq);
//			}else{
//				if(item.getString("payment_app_code").isEmpty()){
//					
//				}
//			costCheckListService.saveCostCheckList(pdq);
//			}

//			 pp.getString("PaymentCode");
			PageData pdq = new PageData();			
			if(item.getString("invoice_code").equals("null") || item.getString("invoice_code").equals("") ||item.getString("invoice_code")=="" || item.getString("invoice_code")==null){
				if(item.getString("payment_app_code")==null||item.getString("payment_app_code")==""||item.getString("payment_app_code").equals("null")||item.getString("payment_app_code").equals("")){
					pdq.put("payment_app_code", pp1);
					
				}else{
					pdq.put("payment_app_code", item.getString("payment_app_code"));
				}
				
				pdq.put("loan_bill_code", item.getString("loan_odd_code"));
				if(item.getString("loan_date")==null||item.getString("loan_date")==""||item.getString("loan_date").equals("null")||item.getString("loan_date").equals("")){
					
				}else{
					pdq.put("payment_bill_date", item.getString("loan_date"));
				}
				pdq.put("bill_code", item.getString("bill_code"));
				pdq.put("currency", pd.getString("currency"));

				if(item.getString("payment_amount")==null || item.getString("payment_amount")==""||item.getString("payment_amount").equals("")||item.getString("payment_amount").equals("null")){
					
				}else{
					float a = Float.parseFloat(item.getString("payment_amount"));
					pdq.put("amount", a);
				}
				pdq.put("fee_name", item.getString("fee_name"));
				if(item.getString("loan_id").equals("null")||item.getString("loan_id").equals("") ||item.getString("loan_id")=="" || item.getString("loan_id")==null){
					costCheckListService.saveCostLoanDetail(pdq);
				}else{
					pdq.put("id", item.getString("loan_id"));
//					PageData record=costCheckListService.findCostInfoLoanById(item.getString("loan_id"));
//					costCheckListService.saveCostLoanRecordDetail(record);
//					costCheckListService.deleteCostInfoLoan(item.getString("loan_id"));
					costCheckListService.updateCostLoanDetail(pdq);
				}
				
			}else{
				
				pdq.put("payment_app_code", pp1);
//				pdq.put("payment_app_code", pd.getString("payment_app_code"));
				pdq.put("payment_code", item.getString("loan_odd_code"));
				pdq.put("invoice_code", item.getString("invoice_code"));
				pdq.put("bill_code", item.getString("bill_code"));
				pdq.put("currency", pd.getString("currency"));
				pdq.put("amount", item.getString("payment_amount"));
				pdq.put("fee_name", item.getString("fee_name"));
				if(item.getString("id").equals("null")||item.getString("id").equals("") ||item.getString("id")=="" || item.getString("id")==null){
					pdq.put("is_income_loan", 1);
					costCheckListService.saveCostDetail(pdq);
				}else{
					pdq.put("is_income_loan", 1);
					pdq.put("id", item.getString("id"));
					costCheckListService.updateCostDetail(pdq);
				}
				PageData pdqw = new PageData();
				pdqw.put("invoice_code", item.getString("invoice_code"));
				pdqw.put("bill_code", item.getString("bill_code"));
				pdqw.put("loan_bill_code", item.getString("loan_odd_code"));
				pdqw.put("payment_bill_date", item.getString("loan_date"));
				if(item.getString("loan_id").equals("null")||item.getString("loan_id").equals("") ||item.getString("loan_id")=="" || item.getString("loan_id")==null){
					costCheckListService.saveCostLoanDetail(pdqw);
				}else{
					pdqw.put("id", item.getString("loan_id"));
//					PageData record=costCheckListService.findCostInfoLoanById(item.getString("loan_id"));
//					costCheckListService.saveCostLoanRecordDetail(record);
//					costCheckListService.deleteCostInfoLoan(item.getString("loan_id"));
					costCheckListService.updateCostLoanDetail(pdqw);
				}
//				pdqw.put("amount", pd.getString("payment_amount"));
//				pdqw.put("actual_amount", pd.getString("actual_amount"));
			}
		}
		PageData pdqw = new PageData();
		pdqw.put("payment_app_code", pp1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		pdqw.put("payment_bill_date", pd.getString("payment_bill_date"));
		pdqw.put("payment_app_dept", pd.getString("payment_app_dept"));
		pdqw.put("dept_business_type", pd.getString("dept_business_type"));
		pdqw.put("settlement_dept", pd.getString("settlement_dept"));
		pdqw.put("deposit_bank", pd.getString("deposit_bank"));
		pdqw.put("bank_account", pd.getString("bank_account"));
		pdqw.put("remark", pd.getString("remark"));
		pdqw.put("currency", pd.getString("currency"));		
		if(pd.getString("payment_amount")!=null&&pd.getString("payment_amount")!=""){
			float a = Float.parseFloat(pd.getString("payment_amount"));
			pdqw.put("amount", a);
		}
		if(pd.getString("paymentId").isEmpty()||pd.getString("paymentId")==null||pd.getString("paymentId")==""){
			pdqw.put("is_income_loan", 1);
			pdqw.put("actual_amount", pd.getString("actual_amount"));
			costCheckListService.saveCostInfo(pdqw);
		}else{
			pdqw.put("is_income_loan", 1);
			pdqw.put("payment_app_code", pd.getString("payment_app_code"));
			pdqw.put("actual_amount", pd.getString("actual_amount"));
			costCheckListService.updateCostData(pdqw);
		}
		return null;
		
	}
	/**
	 * 保存收入信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveIncomeInfo")
	@ResponseBody
	public String saveIncomeInfo()throws Exception{
			PageData pd = new PageData();
			pd = this.getPageData();
			String data = pd.getString("array");

			JSONArray items = JSONArray.fromObject(data);
			String pp1 = null;
			if(pd.getString("payment_app_code")==null||pd.getString("payment_app_code")==""||pd.getString("payment_app_code").equals("null")||pd.getString("payment_app_code").equals("")){
				PageData b = new PageData();
				b.put("paymentCode", null);
				Object pp = costCheckListService.getPaymentCode(b);
				pp1 = pp.toString();
				
			}else{
				pp1 = pd.getString("payment_app_code");
			}
			for(int i=0,size=items.size();i<size;i++){
				String costId = this.get32UUID();
				JSONObject item = items.getJSONObject(i);
				PageData pdq = new PageData();
				pdq.put("payment_app_code", pp1);
				pdq.put("payment_code", item.getString("loan_odd_code"));
				pdq.put("invoice_code", item.getString("invoice_code"));
				pdq.put("bill_code", item.getString("bill_code"));
				pdq.put("currency", pd.getString("currency"));
				pdq.put("amount", item.getString("payment_amount"));
				pdq.put("fee_name", item.getString("fee_name"));
				if(item.getString("costId").equals("null")||item.getString("costId").equals("") ||item.getString("costId")=="" || item.getString("costId")==null){
					pdq.put("is_income_loan", 0);
					pdq.put("cost_id", costId);
					costCheckListService.saveCostDetail(pdq);
				}else{
					pdq.put("id", item.getString("costId"));
					pdq.put("is_income_loan", 0);
					costCheckListService.updateCostDetail(pdq);
				}
				
				PageData pdqw = new PageData();
				pdqw.put("payment_app_code", pp1);
				pdqw.put("payment_code", item.getString("loan_odd_code"));
				pdqw.put("invoice_code", item.getString("receipt_code"));
				pdqw.put("bill_code", item.getString("receipt_date"));
				pdqw.put("currency", pd.getString("currency"));
//				pdqw.put("amount", item.getString("receipt_amount"));
				if(!item.getString("receipt_amount").equals("null")&&!item.getString("receipt_amount").equals("") && item.getString("receipt_amount")!="" && item.getString("receipt_amount")!=null){
					float a = Float.parseFloat(item.getString("receipt_amount"));
					pdqw.put("amount", a);
				}
				if(item.getString("incomeId").equals("null")||item.getString("incomeId").equals("") ||item.getString("incomeId")=="" || item.getString("incomeId")==null){
					pdqw.put("is_income_loan", 0);
					pdqw.put("cost_id", costId);
					costCheckListService.saveCostIncomeDetail(pdqw);
				}else{
					pdqw.put("id", item.getString("incomeId"));
					pdqw.put("is_income_loan", 0);
					costCheckListService.updateCostIncomeDetail(pdqw);
				}
			}
			PageData pdqw = new PageData();
			pdqw.put("payment_app_code", pp1);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			pdqw.put("payment_bill_date", df.format(new Date()));
			pdqw.put("payment_app_dept", pd.getString("payment_app_dept"));
			pdqw.put("dept_business_type", pd.getString("dept_business_type"));
			pdqw.put("settlement_dept", pd.getString("settlement_dept"));
			pdqw.put("deposit_bank", pd.getString("deposit_bank"));
			pdqw.put("bank_account", pd.getString("bank_account"));
			pdqw.put("remark", pd.getString("remark"));
			pdqw.put("currency", pd.getString("currency"));
			
			if(pd.getString("payment_amount")!=null&&pd.getString("payment_amount")!=""){
				float a = Float.parseFloat(pd.getString("payment_amount"));
				pdqw.put("amount", a);
			}
			if(pd.getString("paymentId").isEmpty()||pd.getString("paymentId")==null||pd.getString("paymentId")==""){
				pdqw.put("is_income_loan", 0);
				pdqw.put("actual_amount", pd.getString("actual_amount"));
				costCheckListService.saveCostInfo(pdqw);
			}else{
				pdqw.put("payment_app_code", pd.getString("payment_app_code"));
				pdqw.put("is_income_loan", 0);
				pdqw.put("actual_amount", pd.getString("actual_amount"));
				costCheckListService.updateCostData(pdqw);
			}
		return null;
	}
	@Override
	public List getData(String type, Map param, Page page) {
		PageData pd = new PageData();

		List<PageData> map = new ArrayList<PageData>();
		String data = param.get("ids").toString();
		List<String> result = Arrays.asList(data.split(","));
		for(String id : result){
			PageData pdq;
			try {
				pdq = costCheckListService.findCostCheckListById(id);
				map.add(pdq);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		
		
		
//		JSONArray items = JSONArray.fromObject(data);
//		
//		for(int i=0,size=items.size();i<size;i++){
//			JSONObject item = items.getJSONObject(i);
//			String id = item.getString("id").trim();
//			try {
//				PageData pdq = costCheckListService.findCostCheckListById(id);
//				map.add(pdq);
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return map;
	}
	@Override
	public String getService() {
		return  getClass().getSimpleName();
	}
	@Override
	public Map<String, String> setNames() {
		Map m = new HashMap(3);
		//保险申报单
		m.put("costCheckList","costCheckList.xls");
		m.put("costCheckListName","保险申报单("+ DateUtil.getSdfTimes()+").xls");
		m.put("costCheckListModule",getService());
		return m;
	}
}
